package com.cupk.controllers;

import com.cupk.dtos.ChangePasswordRequest;
import com.cupk.dtos.MessageResponse;
import com.cupk.dtos.UpdateUserRequest;
import com.cupk.dtos.UserDto;
import com.cupk.services.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户个人资料控制器 - 兼容前端的 /user 路径
 * 这个控制器提供与 UserController 相同的功能，但使用 /user 路径前缀
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserProfileController {

    private final UserService userService;

    /**
     * 获取当前用户的个人资料
     */
    @GetMapping("/profile")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<UserDto> getCurrentUserProfile(Authentication authentication) {
        String username = authentication.getName();
        UserDto userProfile = userService.getUserProfile(username);
        return ResponseEntity.ok(userProfile);
    }

    /**
     * 更新当前用户的个人资料
     */
    @PutMapping("/profile")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<UserDto> updateCurrentUserProfile(
            @Valid @RequestBody UpdateUserRequest request,
            Authentication authentication) {
        String username = authentication.getName();
        log.info("收到更新个人资料请求，用户: {}", username);
        log.info("请求数据详情: fullName={}, email={}, phoneNumber={}, department={}, status={}",
                request.getFullName(), request.getEmail(), request.getPhoneNumber(),
                request.getDepartment(), request.getStatus());
        UserDto updatedProfile = userService.updateUserProfile(username, request);
        log.info("更新完成，返回数据: {}", updatedProfile);
        return ResponseEntity.ok(updatedProfile);
    }

    /**
     * 修改当前用户的密码
     */
    @PostMapping("/change-password")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<MessageResponse> changePassword(
            @Valid @RequestBody ChangePasswordRequest request,
            Authentication authentication) {
        String username = authentication.getName();
        userService.changePassword(username, request);
        return ResponseEntity.ok(new MessageResponse("密码修改成功"));
    }

    /**
     * 修改当前用户的密码 - PUT方法兼容
     */
    @PutMapping("/change-password")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<MessageResponse> changePasswordPut(
            @Valid @RequestBody ChangePasswordRequest request,
            Authentication authentication) {
        String username = authentication.getName();
        userService.changePassword(username, request);
        return ResponseEntity.ok(new MessageResponse("密码修改成功"));
    }

    @PostMapping("/avatar")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<Map<String, String>> uploadAvatar(
            @RequestParam("avatar") MultipartFile file,
            Authentication authentication) {

        String username = authentication.getName();
        String avatarUrl = userService.updateUserAvatar(username, file);

        Map<String, String> response = new HashMap<>();
        response.put("avatarUrl", avatarUrl);

        return ResponseEntity.ok(response);
    }
}
